package xyz.xtt.equity.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xyz.xtt.asset.client.response.AssetAllInfoResponse;
import xyz.xtt.asset.client.response.AssetExtInfoResponse;
import xyz.xtt.asset.client.response.AssetInfoResponse;
import xyz.xtt.asset.client.response.AssetStaticResponse;
import xyz.xtt.common.starter.cache.CacheManager;
import xyz.xtt.equity.constants.AssetCache;
import xyz.xtt.equity.entity.TblBenefitPackage;
import xyz.xtt.equity.mapper.TblBenefitPackageMapper;
import xyz.xtt.equity.service.ITblBenefitPackageService;

/**
 * <p>
 * 效益售卖包 服务实现类
 * </p>
 *
 * @author dale
 * @since 2025-02-10
 */
@Service
public class TblBenefitPackageServiceImpl extends ServiceImpl<TblBenefitPackageMapper, TblBenefitPackage> implements ITblBenefitPackageService {
	@Autowired
	private CacheManager cacheManager;

	@Override
	public List<TblBenefitPackage> getNftVipPackages() {
		return cacheManager.cacheList(() -> {
			LambdaQueryChainWrapper<TblBenefitPackage> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
			return queryChainWrapper.eq(TblBenefitPackage::getPackageType, "NFT_VIP").list();
		}, AssetCache.BENEFIT_PACKAGE_LIST_BY_CODE, "NFT_VIP");
	}

	@Override
	public TblBenefitPackage getNftVipPackage(Long packageId) {
		return cacheManager.cacheObj(() -> {
			LambdaQueryChainWrapper<TblBenefitPackage> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
			return queryChainWrapper.eq(TblBenefitPackage::getPackageId, packageId).one();
		}, AssetCache.BENEFIT_PACKAGE, String.valueOf(packageId));
	}

	/**
	 * VIP信息伪装成资产
	 * 
	 * @param benefitPackageId
	 * @return
	 */
	@Override
	public AssetAllInfoResponse baseInfo(Long benefitPackageId) {
		AssetAllInfoResponse assetBaseInfo = AssetAllInfoResponse
				.builder()
				.asset(new AssetInfoResponse())
				.assetExt(new AssetExtInfoResponse())
				.assetStatic(new AssetStaticResponse())
				.build();
		List<TblBenefitPackage> nftVipPackages = getNftVipPackages();
		nftVipPackages.stream().filter(x -> x.getPackageId().equals(benefitPackageId)).findFirst().ifPresent(x -> {
			assetBaseInfo.getAsset().setAssetName(x.getPackageName());
			assetBaseInfo.getAsset().setId(0L);
			assetBaseInfo.getAsset().setGoodsId("");
			assetBaseInfo.getAssetStatic().setLogoUri(StringUtils.hasText(x.getImageUrl()) ? x.getImageUrl() : "");
			assetBaseInfo.getAsset().setFirstType(0);
			assetBaseInfo.getAsset().setSecondType(0);
		});
		return assetBaseInfo;
	}

	@Override
	public TblBenefitPackage getPackageByCode(String packageCode) {
		return cacheManager.cacheObj(() -> {
			LambdaQueryChainWrapper<TblBenefitPackage> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
			return queryChainWrapper.eq(TblBenefitPackage::getPackageCode, packageCode).eq(TblBenefitPackage::getIsDel, 0).one();
		}, AssetCache.BENEFIT_CODE_PACKAGE, packageCode);
	}

	@Override
	public TblBenefitPackage getPackageById(Long packageId) {
		return cacheManager.cacheObj(() -> {
			LambdaQueryChainWrapper<TblBenefitPackage> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
			return queryChainWrapper.eq(TblBenefitPackage::getPackageId, packageId).eq(TblBenefitPackage::getIsDel, 0).one();
		}, AssetCache.BENEFIT_ID_PACKAGE, "" + packageId);
	}
}
